<template>
    <div>
        <el-dialog :title="type==1?'分配个人':'分配线索池'" width="500px" :visible.sync="dialogVisible">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col v-if="type==1" :span="24">
                        <el-form-item label="接收用户" prop="member_id">
                            <el-select v-model="form.member_id" placeholder="请选择人员" style="width: 100%;" @change="refreshData">
                                <el-option v-for="(item, index) in memberList" :key="index" :label="item.realname" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col v-if="type==2" :span="24">
                        <el-form-item label="接收线索池" prop="pool">
                            <el-select v-model="form.pool" placeholder="请选择线索池" style="width: 100%;" @change="refreshData">
                                <el-option v-for="item, index in dict.poolList" :key="index" :label="item.name" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { mapState } from 'vuex'
import { fenpei } from '@/api/kehu'
import { memberList } from '@/api/system'
export default {
    name: 'UserTransferDialog',
    data() {
        return {
            dialogVisible: false,
            type: 1,
            memberList: [],
            form: {
                member_id: ''
                // pool: ''
            },
            rules: {
                member_id: [{ required: true, message: '请选择人员', trigger: 'change' }],
                pool: [{ required: true, message: '请选择线索池', trigger: 'change' }]
            }
        }
    },
    computed: {
        ...mapState(["dict"])
    },
    methods: {
        showDialog(list, type) {
            if (list.length > 0) {
                let ids = list.map(item => {
                    return item.id
                })
                this.form.ids = ids.join(',');
                // this.type = type;
                this.dialogVisible = true;
                // this.resetParams();
                this.resetForm();
                this.getOptions();
            }
        },
        resetForm() {
            this.$nextTick(() => {
                this.$refs.ruleForm.resetFields();
            });
        },
        resetParams() {
            this.form.member_id = '';
            this.form.pool = '';
        },
        handleSubmit(e) {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    fenpei(this.form).then(res => {
                        if (res.status == '1') {
                            this.$message.success('操作成功！')
                            this.$emit('success', res)
                        } else {
                            this.$message.error(res.message)
                        }
                    }).finally(() => {
                        this.dialogVisible = false
                    })
                }
            })
        },
        // 获取 options
        getOptions() {
            memberList().then(res => {
                this.memberList = res.data.list || []
            })
        },
        refreshData(val) {
            this.$forceUpdate()
        }
    }
}
</script>